package server.command;

import java.util.Iterator;
import java.util.List;

import org.apache.mina.core.session.IoSession;

import server.IDispatcher;
import server.Message;
import server.core.Checkpoint;
import server.dao.CheckPointDAO;
import server.dao.DAOFactory;

public class Checkpoints extends Command {

	private Checkpoints() {
	}

	public static Command getInstance() {
		if (instance == null)
			instance = new Checkpoints();
		return instance;
	}

	@Override
	public synchronized void execute(IoSession session, List<String> parameters, IDispatcher dispatcher) {
		DAOFactory df = DAOFactory.getInstance();
		CheckPointDAO checkpointDAO = df.getCheckPointDAO();
		String result = "ERROR1";//"Operation failed!";

		if (parameters.size() == 3) {
			Iterator<String> it = parameters.iterator();
			int length = Integer.parseInt(it.next());
			int width = Integer.parseInt(it.next());
			int distance = Integer.parseInt(it.next());
			List<Checkpoint> list = checkpointDAO.getNearestCheckpoints(length, width, distance);
			Iterator<Checkpoint> it2 = list.iterator();
			if (it2.next().equals("ERROR")) {
				result = "ERROR2";
			} else {
				result = "";
				while (it2.hasNext()) {
					result = result + it2.hasNext();
				}
			}
		}
		session.write(new Message("CHECKPOINTS", result));
	}
}
